using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class PlayerAttack : MonoBehaviour
{
    private int damage = 3;

    private float dis = 2f;

    private GameObject[] GetCanAttackenemys(float dis)
    {
        GameObject[] enemys = GameObject.FindGameObjectsWithTag("Enemy");

        if (enemys == null || enemys.Length == 0)
        {
            return null;
        }

        Transform player = this.transform;

        List<GameObject> list = new List<GameObject>();
        for(int i = 0;i < enemys.Length; i++)
        {
            float d = Vector2.Distance(enemys[i].transform.position, player.transform.position);
            if (d < dis && enemys[i].GetComponent<Enemy>().is_Died == false){
                list.Add(enemys[i]);
            }
        }
        return list.ToArray();
    }

    private GameObject[] GetCanAttackBoss(float dis)
    {
        GameObject[] bosses = GameObject.FindGameObjectsWithTag("Boss");

        if (bosses == null || bosses.Length == 0)
        {
            return null;
        }

        Transform player = this.transform;

        List<GameObject> list = new List<GameObject>();
        for (int i = 0; i < bosses.Length; i++)
        {
            float d = Vector2.Distance(bosses[i].transform.position, player.transform.position);
            if (d < dis)
            {
                list.Add(bosses[i]);
            }
        }
        return list.ToArray();
    }

    private GameObject[] GetCanReleasehostage(float dis)
    {
        GameObject[] hos = GameObject.FindGameObjectsWithTag("hostage");

        if(hos == null || hos.Length == 0)
        {
            return null;
        }

        Transform player = this.transform;
        
        List <GameObject> list = new List<GameObject>();
        for(int i = 0;i < hos.Length; i++)
        {
            float d = Vector2.Distance(hos[i].transform.position, player.transform.position);
            if(d < dis && hos[i].gameObject.GetComponent<hostage>().is_release == false)
            {
                list.Add(hos[i]);
            }
        }
        return list.ToArray() ;
    }

    public bool HurtEnemys()
    {
        GameObject[] enemys = GetCanAttackenemys(dis);
        GameObject[] bosses = GetCanAttackBoss(dis);
        GameObject[] hos = GetCanReleasehostage(dis);
        bool flag = false;
        if(enemys!=null && enemys.Length != 0)
        {
            foreach (var enemy in enemys)
            {
                enemy.GetComponent<Enemy>().Hurt();
            }
            flag = true;
        }
        if(bosses!=null && bosses.Length != 0)
        {
            foreach(var boss in bosses)
            {
                boss.GetComponent<Boss>().Hurt(damage);
            }
            flag = true;
        }
        if(hos != null && hos.Length != 0)
        {
            foreach(var hostage in hos)
            {
                hostage.GetComponent<hostage>().release();
            }
            flag = true;
        }
        
        return flag;
    }

}
